#pragma GCC optimize(2)
#include <iostream>
#include <queue>
#include <map>
#include <unordered_map>
#include <vector>
#include <algorithm>
#include <cmath>
#include <string>
#include <cstring>
#include <set>
#include <unordered_set>
#include <stack>
#include <bitset>
#define F first
#define S second
#define IOS ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
void solve()
{
	int n,k;
	cin>>n>>k;
	vector<vector<int> > a(n+1,vector<int> (n+1));
	vector<vector<int> > b(n+1,vector<int> (n+1));
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=n;j++)
		{
			cin>>a[i][j];
			b[n-i+1][n-j+1]=a[i][j];
		}
	}
	if(n==1)
	{
		cout<<"YES"<<endl;
		return ;
	}
	int cnt=0;
	for(int i=1;i<=(n+1)/2;i++)
	{
		int num=0;
		for(int j=1;j<=n;j++)
		{
			if(n%2 && i==(n+1)/2)
			{
				if(a[i][j]!=b[i][j]) num++;
				continue;
			}
			if(a[i][j]!=b[i][j]) cnt++;
		}
		if(n%2 && i==(n+1)/2) cnt+=num/2;
	}
//	cout<<cnt<<endl;
	if(cnt>k) cout<<"NO"<<endl;
	else if((k-cnt)%2)
	{
		if(n%2 && k>cnt) cout<<"YES"<<endl;
		else cout<<"NO"<<endl;
	}
	else cout<<"YES"<<endl;
}
int main()
{
	IOS;
	int T=1;
	cin>>T;

	while(T--)
	{
		solve();
	}

	return 0;
}

